/** 
 * 
 * @author geloin 
 * @date 2012-5-5 涓婂崍9:31:52 
 */
package com.wcs.spring.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcs.spring.entity.School;
import com.wcs.spring.service.SchoolService;
import com.wcs.spring.util.PageUtil;

@Controller
@RequestMapping(value = "mgt/school")
public class SchoolController {
	@Resource(name = "schoolService")
	private SchoolService schoolService;

	@RequestMapping(value = "schoolList")
	public ModelAndView schoolList(School school, PageUtil pageUtil, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<School> list = this.schoolService.querySchoolList(school, pageUtil);

		map.put("schoolList", list);
		return new ModelAndView("mgt/school/school_list", map);
	}

	@RequestMapping(value = "toSchoolSave")
	public ModelAndView toSchoolSave(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/school/school_add");
	}

	@RequestMapping(value = "toSchoolUpdate")
	public ModelAndView toSchoolUpdate(Integer id, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		School school = schoolService.querySchoolById(id);
		map.put("school", school);
		return new ModelAndView("mgt/school/school_add", map);
	}

	@RequestMapping(value = "toSchoolShow")
	public @ResponseBody
	School toSchoolShow(Integer id, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		School school = schoolService.querySchoolById(id);
		// map.put("school", school);
		// return new ModelAndView("mgt/school/school_show", map);
		return school;
	}

	@RequestMapping(value = "schoolSave")
	public ModelAndView schoolSave(School school, HttpServletResponse response) throws Exception {
		Date date = new Date();
		school.setCreateTime(date);
		school.setUpdateTime(date);
		school.setCreateUser("admin");
		school.setUpdateUser("admin");
		schoolService.addSchool(school);
		return new ModelAndView("redirect:/mgt/school/schoolList.do");
	}

	@RequestMapping(value = "schoolUpdate")
	public ModelAndView schoolUpdate(School school, HttpServletResponse response) throws Exception {
		school.setUpdateTime(new Date());
		school.setUpdateUser("hexuan");

		schoolService.updateSchool(school);
		return new ModelAndView("redirect:/mgt/school/schoolList.do");
	}

	@RequestMapping(value = "schoolDelete")
	public ModelAndView schoolDelete(School school, HttpServletResponse response) throws Exception {
		schoolService.delSchool(school);
		return new ModelAndView("redirect:/mgt/school/schoolList.do");
	}

	@RequestMapping(value = "toSchoolSimple")
	public @ResponseBody
	Map<String, Object> toSchoolSimple(HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<School> schoolList = schoolService.querySchoolSimple();
		map.put("schoolList", schoolList);
		return map;
	}

}
